<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <!-- 点击搜索跳转到搜索页面 -->
      <van-button class="search-btn" slot="title" type="info" size="small" round
        icon="search" to="/search">搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->
    <!--
    使用的组件属性说明:
    通过v-model绑定当前激活标签对应的索引值，默认情况下启用第一个标签
    通过animated属性可以开启切换标签内容时的转场动画
    通过swipeable属性可以开启滑动切换标签页
-->
    <van-tabs v-model="active" class="channel-tabs" animated swipeable>
      <van-tab :title="channel.name" v-for="channel in channels"
        :key="channel.id">
        <!-- 频道的文章列表，每个频道相对应着自己的频道列表内容 -->
        <article-list ref="article-list" :channel="channel" />
      </van-tab>
      <div slot="nav-right" class="placeholder">
      </div>
      <!-- 汉堡按钮 -->
      <div slot="nav-right" class="hamburger-btn">
        <i @click="isChennelEditShow=true" class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!--点击汉堡按钮时，出现的弹出层。 频道编辑 -->
    <van-popup v-model="isChennelEditShow" position="bottom"
      :style="{ height: '100%' }" closeable close-icon-position="top-left">
      <!-- 这个组件用来放置弹出层里面的内容；传递active高亮的索引 ；监听update-active事件-->
      <Channel-edit :myChannels='channels' :active='active'
        @update-active="onUpdateActive" />
    </van-popup>
    <!-- /频道编辑 -->
  </div>
</template>

<script>
// 首页频道列表对应的文章内容的组件
import ArticleList from './components/article-list'
// 这个组件用来储存弹出层里面的内容
import ChannelEdit from '@/views/home/components/channel-edit'
// 导入 获取频道列表的方法
import { getUserChannels } from '@/api/user'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0, // 借助这个变量 来 标识当前所选中的频道
      channels: [], //  定义数据接收频道列表
      isChennelEditShow: false // 点击汉堡按钮时，弹层是否出现
    }
  },
  computed: {
    // 映射为计算属性
    ...mapState(['user'])
  },
  watch: {},
  created () {
    //  初始化时调用获取频道列表
    this.loadChannels()
  },
  mounted () { },
  methods: {
    async loadChannels () { // 定义加载频道列表数据的方法
      try {
        let channels = []
        const localChannels = getItem('TOUTIAO_CHANNELS')
        // 登录了就发起请求获取数据，未登录但是本地也没有，也需要请求固定的数据
        // 未登录，但是本地有，就从本地得到
        if (this.user || !localChannels) {
          const res = await getUserChannels()
          channels = res.data.data.channels
        } else {
          channels = localChannels
        }
        this.channels = channels
        // console.log(this.channels)
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    onUpdateActive (index, isChennelEditShow = true) { // 监听到子组件update-active方法是，执行该函数
      this.active = index// 弹出层我的频道里对应首页的频道切换，关闭弹出层
      this.isChennelEditShow = isChennelEditShow
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  /*添加上padding 撑开高度*/
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
      width: 100%;
    }
  }
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /* 添加tabs样式 */
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      height: 82px;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background: #3296fa;
    }
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 58px;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>
